using System;
using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class CareerSettings : ScriptableObjectSingleton<CareerSettings>
{
	[Serializable]
	public class CareerGroup
	{
		public string groupName;

		public List<CareerOpponent> opponents;

		protected CareerGroupDAO model;

		public bool isActive
		{
			get
			{
				return model.isActive;
			}
			set
			{
				model.isActive = value;
			}
		}

		public bool isPassed
		{
			get
			{
				foreach (CareerOpponent opponent in opponents)
				{
					if (opponent.starCount == 0)
					{
						return false;
					}
				}
				return true;
			}
		}

		public int collectedStarCount
		{
			get
			{
				int num = 0;
				foreach (CareerOpponent opponent in opponents)
				{
					num += opponent.starCount;
				}
				return num;
			}
		}

		public int totalStarCount
		{
			get
			{
				return 3 * opponents.Count;
			}
		}

		public void Init(CareerGroupDAO model)
		{
			this.model = model;
			if (model.stages == null)
			{
				model.stages = new List<CareerGroupStageDAO>();
			}
			CareerOpponent previous = null;
			for (int i = 0; i < opponents.Count; i++)
			{
				if (i >= model.stages.Count)
				{
					model.stages.Add(new CareerGroupStageDAO());
				}
				opponents[i].Init(model.stages[i], this, previous);
				previous = opponents[i];
			}
		}
	}

	[Serializable]
	public class CareerOpponent : GameDoneListener
	{
		public GameConstants.Flags flag;

		public string name;

		public int scoreDiffFor2Stars;

		public int scoreDiffFor3Stars;

		public RoomLook roomLook;

		public int aiDefinitionIndex;

		public int pointsToWinGame = 11;

		public int gamesToWinMatch = 1;

		[NonSerialized]
		private CareerOpponent previous;

		[NonSerialized]
		private CareerGroup group_;

		protected CareerGroupStageDAO model;

		public int starCount
		{
			get
			{
				return model.stars;
			}
			set
			{
				model.stars = value;
			}
		}

		public bool isActive
		{
			get
			{
				return previous == null || previous.starCount > 0;
			}
		}

		public CareerGroup group
		{
			get
			{
				return group_;
			}
			protected set
			{
				group_ = value;
			}
		}

		public int StarCountForScoreDiff(int scoreDiff)
		{
			if (scoreDiff < 0)
			{
				return 0;
			}
			if (scoreDiff >= scoreDiffFor3Stars)
			{
				return 3;
			}
			if (scoreDiff >= scoreDiffFor2Stars)
			{
				return 2;
			}
			return 1;
		}

		public void Init(CareerGroupStageDAO model, CareerGroup group, CareerOpponent previous)
		{
			this.model = model;
			this.group = group;
			this.previous = previous;
		}

		public MatchParameters.MatchParams ToMatchParameters()
		{
			MatchParameters.MatchParams matchParams = MatchParameters.MatchParams.CreateDefault();
			matchParams.player2Name = name;
			matchParams.player2Flag = (int)flag;
			matchParams.matchType = MatchType.SinglePlayer;
			matchParams.aiDefinition = ScriptableObjectSingleton<AiPlayerDefinitions>.instance.GetDefinition(aiDefinitionIndex);
			matchParams.pointsToWinGame = pointsToWinGame;
			matchParams.gamesToWinMatch = gamesToWinMatch;
			matchParams.roomLook = (int)roomLook;
			matchParams.gameDoneListener = this;
			return matchParams;
		}

		void GameDoneListener.OnGameDone(MatchController match)
		{
			SingleTableTennis gameType = match.gameType;
			bool flag = gameType.playerThatWon.teamTag == 0;
			int num = Mathf.Max(0, gameType.GetTotalPointsDifference());
			if (!flag)
			{
				num = -1;
			}
			Debug.Log("Game Done Listener Notified!!! " + num);
			ScriptableObjectSingleton<CareerSettings>.instance.UpdateCareerWithGame(this, num);
		}
	}

	public List<CareerGroup> groups = new List<CareerGroup>();

	private CareerDAO model;

	private const string filename = "careerDao.bytes";

	protected override void Init()
	{
		ReloadModel();
	}

	public void ReloadModel()
	{
		if (!ProtoIO.LoadFromFileCloudSync<CareerDAO>("careerDao.bytes", out model))
		{
			model = new CareerDAO();
		}
		if (model.groups == null)
		{
			model.groups = new List<CareerGroupDAO>();
		}
		for (int i = 0; i < groups.Count; i++)
		{
			if (i >= model.groups.Count)
			{
				model.groups.Add(new CareerGroupDAO());
			}
			groups[i].Init(model.groups[i]);
		}
		model.groups[0].isActive = true;
		CheckPassedGroupsAndActivateNewOnes();
		ProtoIO.SaveToFile("careerDao.bytes", model);
	}

	protected void CheckPassedGroupsAndActivateNewOnes()
	{
		for (int i = 1; i < groups.Count; i++)
		{
			if (groups[i - 1].isPassed)
			{
				groups[i].isActive = true;
			}
		}
	}

	public void Save()
	{
		ProtoIO.SaveToFileCloudSync("careerDao.bytes", model);
	}

	public void UpdateCareerWithGame(CareerOpponent opponent, int scoreDiff)
	{
		CareerGroup group = opponent.group;
		opponent.starCount = Mathf.Max(opponent.starCount, opponent.StarCountForScoreDiff(scoreDiff));
		CheckPassedGroupsAndActivateNewOnes();
		Save();
	}
}
